<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <title>配置表格</title>
    <link rel="stylesheet" type="text/css" href="${s.base}/lib/layui/css/layui.css"/>
    <link rel="stylesheet" type="text/css" href="${s.base}/css/base/index.css"/>
    <link rel="stylesheet" type="text/css" href="${s.base}/css/base/theme/theme.css"/>
</head>
<style>
    .w100 {
        width: 90px !important;
    }

    .w120 {
        width: 120px !important;
    }

    .w150 {
        width: 150px !important;
    }

    .w180 {
        width: 180px !important;
    }

    .layui-btn-disabled, .layui-btn-disabled:active, .layui-btn-disabled:hover {
        border: 1px solid #e6e6e6 !important;
        background-color: #FBFBFB !important;
        color: #C9C9C9 !important;
        cursor: not-allowed !important;
        opacity: 1 !important;
    }
</style>
<body>
<div class="page-content-wrap clearfix">
    <form class="layui-form">
        <div class="layui-tab">
            <div class="layui-tab-content">
                <fieldset class="layui-elem-field layui-field-title">
                    <legend>工具栏</legend>
                </fieldset>
                <div class="layui-tab-item layui-show" id="tool">
                    <div class="layui-form-item">
                        <label class="layui-form-label w100"><span class="red">*</span>&nbsp;添加：</label>
                        <div class="layui-input-inline w120">
                            <#if formTool != null && (formTool.addFunction?substring(0,1)=='1')>
                            <input type="checkbox" name="addFunction" value="1" title="头部工具" checked>
                            <#else>
                            <input type="checkbox" name="addFunction" value="1" title="头部工具">
                            </#if>
                        </div>
                        <div class="layui-input-inline w120">
                            <#if formTool != null && (formTool.addFunction?substring(2,3)=='2')>
                            <input type="checkbox" name="addFunction" value="2" title="表格工具" checked>
                            <#else>
                            <input type="checkbox" name="addFunction" value="2" title="表格工具">
                            </#if>
                        </div>
                        <label class="layui-form-label w100"><span class="red">*</span>&nbsp;编辑：</label>
                        <div class="layui-input-inline w120">
                            <#if formTool != null && (formTool.editFunction?indexOf("1")>-1)>
                            <input type="checkbox" name="editFunction" value="1" title="头部工具" checked>
                            <#else>
                            <input type="checkbox" name="editFunction" value="1" title="头部工具">
                            </#if>
                        </div>
                        <div class="layui-input-inline w120">
                            <#if formTool != null && (formTool.editFunction?indexOf("2")>-1)>
                            <input type="checkbox" name="editFunction" value="2" title="表格工具" checked>
                            <#else>
                            <input type="checkbox" name="editFunction" value="2" title="表格工具">
                            </#if>
                        </div>
                        <label class="layui-form-label w100"><span class="red">*</span>&nbsp;删除：</label>
                        <div class="layui-input-inline w120">
                            <#if formTool != null && (formTool.deleteFunction?indexOf("1")>-1)>
                            <input type="checkbox" name="deleteFunction" value="1" title="头部工具" checked>
                            <#else>
                            <input type="checkbox" name="deleteFunction" value="1" title="头部工具">
                            </#if>
                        </div>
                        <div class="layui-input-inline w120">
                            <#if formTool != null && (formTool.deleteFunction?indexOf("2")>-1)>
                            <input type="checkbox" name="deleteFunction" value="2" title="表格工具" checked>
                            <#else>
                            <input type="checkbox" name="deleteFunction" value="2" title="表格工具">
                            </#if>
                        </div>
                    </div>
                </div>
                <fieldset class="layui-elem-field layui-field-title">
                    <legend>搜索栏</legend>
                </fieldset>
                <div class="layui-tab-item layui-show" id="search">
                    <#assign index=0 size=formSearchVoList?size>
                    <#list formSearchVoList as formSearchVo>
                    <#if formSearchVo.field != 'id' && formSearchVo.field != 'remove_tag'>
                    <div class="layui-form-item">
                        <label class="layui-form-label w100"><span class="red">*</span>&nbsp;字段：</label>
                        <div class="layui-input-inline w120">
                            <input type="text" name="field_${index}" lay-verify="required" value="${formSearchVo.field}" placeholder="请输入字段" class="layui-input">
                        </div>
                        <label class="layui-form-label w100"><span class="red">*</span>&nbsp;名称：</label>
                        <div class="layui-input-inline w120">
                            <input type="text" name="name_${index}" lay-verify="required" value="${formSearchVo.name}" placeholder="请输入名称" class="layui-input">
                        </div>
                        <label class="layui-form-label w100"><span class="red">*</span>&nbsp;类型：</label>
                        <div class="layui-input-inline w120">
                            <select name="type_${index}" lay-filter="type" lay-verify="required">
                                <option value=""<#if (formSearchVo.type == '')>selected</#if>>请选择</option>
                                <option value="input"<#if (formSearchVo.type == 'input')>selected</#if>>输入框</option>
                                <option value="select"<#if (formSearchVo.type == 'select')>selected</#if>>下拉框</option>
                                <option value="dateTime"<#if (formSearchVo.type == 'dateTime')>selected</#if>>日历框</option>
                            </select>
                        </div>
                        <#if formSearchVo.type == 'input'>
                        <label class="layui-form-label w100"><span class="red">*</span>&nbsp;关联：</label>
                        <div class="layui-input-inline w150">
                            <select name="isRelated_${index}" lay-filter="isRelated" lay-verify="required">
                                <option value="1"<#if (formSearchVo.isRelated == '1')>selected</#if>>是</option>
                                <option value="2"<#if (formSearchVo.isRelated != '1')>selected</#if>>否</option>
                        </select>
                        </div>
                        <#if formSearchVo.isRelated == '1'>
                        <label class="layui-form-label w100"><span class="red">*</span>&nbsp;关联表：</label>
                        <div class="layui-input-inline w180">
                            <select name="relatedTable_${index}" lay-filter="relatedTable" lay-verify="required">
                                <option value="">请选择</option>
                                <#list formSearchVo.formTableVoList as formTableVo>
                                <option value="${formTableVo.tableName}"<#if formTableVo.tableName == formSearchVo.relatedTable>selected</#if>>${formTableVo.tableName}</option>
                                </#list>
                            </select>
                        </div>
                        <label class="layui-form-label w100"><span class="red">*</span>&nbsp;关联字段：</label>
                        <div class="layui-input-inline w150">
                            <select name="relatedField_${index}" lay-filter="relatedField" lay-verify="required">
                                <option value="">请选择</option>
                                <#list formSearchVo.formTableFieldVoList as formTableFieldVo>
                                <option value="${formTableFieldVo.field}"<#if formTableFieldVo.field == formSearchVo.relatedField>selected</#if>>${formTableFieldVo.field}</option>
                                </#list>
                            </select>
                        </div>
                        <label class="layui-form-label w100"><span class="red">*</span>&nbsp;关联展示字段：</label>
                        <div class="layui-input-inline w150">
                            <select name="relatedShowField_${index}" lay-verify="required">
                                <option value="">请选择</option>
                                <#list formSearchVo.formTableFieldVoList as formTableFieldVo>
                                <option value="${formTableFieldVo.field}"<#if formTableFieldVo.field == formSearchVo.relatedShowField>selected</#if>>${formTableFieldVo.field}</option>
                                </#list>
                            </select>
                        </div>
                        </#if>
                        </#if>
                        <#if formSearchVo.type == 'select'>
                        <label class="layui-form-label w100"><span class="red">*</span>&nbsp;字典码：</label>
                        <div class="layui-input-inline w150">
                            <select name="code_${index}" lay-verify="required">
                                <option value="">请选择</option>
                                <#list formSearchVo.sysDictList as sysDict>
                                <option value="${sysDict.code}"<#if sysDict.code == formSearchVo.code>selected</#if>>${sysDict.code}</option>
                                </#list>
                            </select>
                        </div>
                        </#if>
                        <#if formSearchVo.type == 'dateTime'>
                        <label class="layui-form-label w100"><span class="red">*</span>&nbsp;格式：</label>
                        <div class="layui-input-inline w150">
                            <select name="format_${index}" lay-verify="required">
                                <option value="">请选择</option>
                                <option value="year"<#if 'year' == formSearchVo.format>selected</#if>>年</option>
                                <option value="month"<#if 'month' == formSearchVo.format>selected</#if>>年月</option>
                                <option value="date"<#if 'date' == formSearchVo.format>selected</#if>>年月日</option>
                                <option value="time"<#if 'time' == formSearchVo.format>selected</#if>>时分秒</option>
                                <option value="datetime"<#if 'datetime' == formSearchVo.format>selected</#if>>年月日时分秒</option>
                            </select>
                        </div>
                        </#if>
                        <button class="layui-btn layui-btn-normal layui-btn-danger" type="button" onclick="deleteField(this,'search')">删除</button>
                        <#if index == 0>
                        <button class="layui-btn layui-btn-normal layui-btn-disabled" type="button" onclick="moveUp(this,'search')">上移</button>
                        <#else>
                        <button class="layui-btn layui-btn-normal" type="button" onclick="moveUp(this,'search')">上移</button>
                        </#if>
                        <#if index == (size-1)>
                        <button class="layui-btn layui-btn-normal layui-btn-disabled" type="button" onclick="moveDown(this,'search')">下移</button>
                        <#else>
                        <button class="layui-btn layui-btn-normal" type="button" onclick="moveDown(this,'search')">下移</button>
                        </#if>
                    </div>
                    <#assign index++>
                    <#else>
                    <#assign size-->
                    </#if>
                    </#list>
                    <div class="layui-form-item">
                        <div class="layui-input-block" style="margin-left: 0px !important;text-align: center">
                            <button class="layui-btn layui-btn-normal" type="button" onclick="addField(this,'search')">添加搜索字段</button>
                        </div>
                    </div>
                </div>
                <fieldset class="layui-elem-field layui-field-title">
                    <legend>表格字段</legend>
                </fieldset>
                <div class="layui-tab-item layui-show" id="show">
                    <#assign index=0 size=formShowVoList?size>
                    <#list formShowVoList as formShowVo>
                    <#if formShowVo.field != 'id' && formShowVo.field != 'remove_tag'>
                    <div class="layui-form-item">
                        <label class="layui-form-label w100"><span class="red">*</span>&nbsp;字段：</label>
                        <div class="layui-input-inline w120">
                            <input type="text" name="field_${index}" lay-verify="required" value="${formShowVo.field}" placeholder="请输入字段" class="layui-input">
                        </div>
                        <label class="layui-form-label w100"><span class="red">*</span>&nbsp;名称：</label>
                        <div class="layui-input-inline w120">
                            <input type="text" name="name_${index}" lay-verify="required" value="${formShowVo.name}" placeholder="请输入名称" class="layui-input">
                        </div>
                        <label class="layui-form-label w100"><span class="red">*</span>&nbsp;类型：</label>
                        <div class="layui-input-inline w120">
                            <select name="type_${index}" lay-filter="type" lay-verify="required">
                                <option value=""<#if (formShowVo.type == '')>selected</#if>>请选择</option>
                                <option value="input"<#if (formShowVo.type == 'input')>selected</#if>>文本</option>
                                <option value="select"<#if (formShowVo.type == 'select')>selected</#if>>字典</option>
                                <option value="dateTime"<#if (formShowVo.type == 'dateTime')>selected</#if>>时间</option>
                            </select>
                        </div>
                        <#if formShowVo.type == 'input'>
                        <label class="layui-form-label w100"><span class="red">*</span>&nbsp;关联：</label>
                        <div class="layui-input-inline w150">
                            <select name="isRelated_${index}" lay-filter="isRelated" lay-verify="required">
                                <option value="1"<#if (formShowVo.isRelated == '1')>selected</#if>>是</option>
                                <option value="2"<#if (formShowVo.isRelated != '1')>selected</#if>>否</option>
                            </select>
                        </div>
                        <#if formShowVo.isRelated == '1'>
                        <label class="layui-form-label w100"><span class="red">*</span>&nbsp;关联表：</label>
                        <div class="layui-input-inline w180">
                            <select name="relatedTable_${index}" lay-filter="relatedTable" lay-verify="required">
                                <option value="">请选择</option>
                                <#list formShowVo.formTableVoList as formTableVo>
                                <option value="${formTableVo.tableName}"<#if formTableVo.tableName == formShowVo.relatedTable>selected</#if>>${formTableVo.tableName}</option>
                            </#list>
                            </select>
                        </div>
                        <label class="layui-form-label w100"><span class="red">*</span>&nbsp;关联字段：</label>
                        <div class="layui-input-inline w150">
                            <select name="relatedField_${index}" lay-filter="relatedField" lay-verify="required">
                                <option value="">请选择</option>
                                <#list formShowVo.formTableFieldVoList as formTableFieldVo>
                                <option value="${formTableFieldVo.field}"<#if formTableFieldVo.field == formShowVo.relatedField>selected</#if>>${formTableFieldVo.field}</option>
                                </#list>
                            </select>
                        </div>
                        <label class="layui-form-label w100"><span class="red">*</span>&nbsp;关联展示字段：</label>
                        <div class="layui-input-inline w150">
                            <select name="relatedShowField_${index}" lay-verify="required">
                                <option value="">请选择</option>
                                <#list formShowVo.formTableFieldVoList as formTableFieldVo>
                                <option value="${formTableFieldVo.field}"<#if formTableFieldVo.field == formShowVo.relatedShowField>selected</#if>>${formTableFieldVo.field}</option>
                                </#list>
                            </select>
                        </div>
                        </#if>
                        </#if>
                        <#if formShowVo.type == 'select'>
                        <label class="layui-form-label w100"><span class="red">*</span>&nbsp;字典码：</label>
                        <div class="layui-input-inline w150">
                            <select name="code_${index}" lay-verify="required">
                                <option value="">请选择</option>
                                <#list formShowVo.sysDictList as sysDict>
                                <option value="${sysDict.code}"<#if sysDict.code == formShowVo.code>selected</#if>>${sysDict.code}</option>
                                </#list>
                            </select>
                        </div>
                        </#if>
                        <#if formShowVo.type == 'dateTime'>
                        <label class="layui-form-label w100"><span class="red">*</span>&nbsp;格式：</label>
                        <div class="layui-input-inline w150">
                            <select name="format_${index}" lay-verify="required">
                                <option value="">请选择</option>
                                <option value="year"<#if 'year' == formShowVo.format>selected</#if>>年</option>
                                <option value="month"<#if 'month' == formShowVo.format>selected</#if>>年月</option>
                                <option value="date"<#if 'date' == formShowVo.format>selected</#if>>年月日</option>
                                <option value="time"<#if 'time' == formShowVo.format>selected</#if>>时分秒</option>
                                <option value="datetime"<#if 'datetime' == formShowVo.format>selected</#if>>年月日时分秒</option>
                            </select>
                        </div>
                        </#if>
                        <button class="layui-btn layui-btn-normal layui-btn-danger" type="button" onclick="deleteField(this,'show')">删除</button>
                        <#if index == 0>
                        <button class="layui-btn layui-btn-normal layui-btn-disabled" type="button" onclick="moveUp(this,'show')">上移</button>
                        <#else>
                        <button class="layui-btn layui-btn-normal" type="button" onclick="moveUp(this,'show')">上移</button>
                        </#if>
                        <#if index == (size-1)>
                        <button class="layui-btn layui-btn-normal layui-btn-disabled" type="button" onclick="moveDown(this,'show')">下移</button>
                        <#else>
                        <button class="layui-btn layui-btn-normal" type="button" onclick="moveDown(this,'show')">下移</button>
                        </#if>
                    </div>
                    <#assign index++>
                    <#else>
                    <#assign size-->
                    </#if>
                    </#list>
                    <div class="layui-form-item">
                        <div class="layui-input-block" style="margin-left: 0px !important;text-align: center">
                            <button class="layui-btn layui-btn-normal" type="button" onclick="addField(this,'show')">添加展示字段</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="margin-left: 0px !important;text-align: center">
                <button class="layui-btn layui-btn-normal" type="button" id="lastStepBtn">上一步</button>
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="previewBtn">预览</button>
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">保存</button>
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="nextStepBtn">下一步</button>
                <button class="layui-btn layui-btn-primary" type="button" id="cancel">取消</button>
            </div>
        </div>
    </form>
</div>
<script src="${s.base}/lib/layui/layui.js" type="text/javascript" charset="utf-8"></script>
<script src="${s.base}/js/common.js" type="text/javascript" charset="utf-8"></script>
<script>
    var form = null;
    var dialog = null;
    layui.use(['form', 'jquery', 'dialog', 'baseutil'], function () {
        var $ = layui.jquery,
            baseutil = layui.baseutil;
        form = layui.form;
        dialog = layui.dialog;

        function showAndInitSelect(data, label, div) {
            var index = 0;
            var child = $(data.othis[0].parentNode.parentNode.children).toArray();
            for (var i = 0; i < child.length; i++) {
                if (child[i] === data.othis[0].parentNode) {
                    index = i;
                }
            }
            var nodeValue = (index === child.length - 4) ? '' : child[index + 1].childNodes[1].nodeValue;
            if (isBlank(label)) {
                if (nodeValue.indexOf('字典码') > -1
                    || nodeValue.indexOf('格式') > -1) {
                    for (var i = index + 3; i < child.length; i++) {
                        child[i - 2] = child[i];
                    }
                    child[child.length - 1] = null;
                    child[child.length - 2] = null;
                } else if (nodeValue.indexOf('关联') > -1
                    || nodeValue.indexOf('关联表') > -1
                    || nodeValue.indexOf('关联字段') > -1) {
                    for (var i = index + 1; i < child.length - 3; i++) {
                        child[i] = null;
                    }
                }
            } else {
                if ((data.value === 'input' && nodeValue.indexOf('关联') === -1)
                    || (data.value === 'readOnly' && nodeValue.indexOf('关联') === -1)
                    || (data.value === 'select' && nodeValue.indexOf('字典码') === -1)
                    || (data.value === 'dateTime' && nodeValue.indexOf('格式') === -1)
                    || (data.value === '1' && nodeValue.indexOf('关联表') === -1)) {
                    for (var i = index + 1; i < child.length - 3; i++) {
                        child[i] = null;
                    }
                    for (var i = child.length - 1; i > index; i--) {
                        child[i + 2] = child[i];
                    }
                    child[index + 1] = $(label)[0];
                    child[index + 2] = $(div)[0];
                }
            }
            $(data.othis[0].parentNode.parentNode).html(child);
            form.render('select');
        }

        //类型下拉框
        form.on('select(type)', function (data) {
            var index = data.elem.attributes['name'].nodeValue.split("_")[1];
            var label = '';
            var div = '';
            if (data.value === 'input') {
                label += '<label class="layui-form-label w100"><span class="red">*</span>&nbsp;关联：</label>';
                div += '<div class="layui-input-inline w150">';
                div += '<select name="isRelated_' + index + '" lay-filter="isRelated" lay-verify="required">';
                div += '<option value="1">是</option>';
                div += '<option value="2" selected>否</option>';
                div += '</select>';
                div += '</div>';
                showAndInitSelect(data, label, div);
            } else if (data.value === 'select') {
                label += '<label class="layui-form-label w100"><span class="red">*</span>&nbsp;字典码：</label>';
                div += '<div class="layui-input-inline w150">';
                div += '<select name="code_' + index + '" lay-verify="required">';
                div += '<option value="">请选择</option>';
                baseutil.AjaxPostMark('/sysDict/list', {}, function (list) {
                    if (list != null && list.length > 0) {
                        for (var i = 0; i < list.length; i++) {
                            div += '<option value="' + list[i].code + '">' + list[i].code + '</option>';
                        }
                    }
                    div += '</select>';
                    div += '</div>';
                    showAndInitSelect(data, label, div);
                });
            } else if (data.value === 'dateTime') {
                label += '<label class="layui-form-label w100"><span class="red">*</span>&nbsp;格式：</label>';
                div += '<div class="layui-input-inline w150">';
                div += '<select name="format_' + index + '" lay-verify="required">';
                div += '<option value="">请选择</option>';
                div += '<option value="year">年</option>';
                div += '<option value="month">年月</option>';
                div += '<option value="date">年月日</option>';
                div += '<option value="time">时分秒</option>';
                div += '<option value="datetime">年月日时分秒</option>';
                div += '</select>';
                div += '</div>';
                showAndInitSelect(data, label, div);
            } else {
                showAndInitSelect(data, label, div);
            }
        });

        //关联下拉框
        form.on('select(isRelated)', function (data) {
            var index = data.elem.attributes['name'].nodeValue.split("_")[1];
            var label = '';
            var div = '';
            if (data.value === '1') {
                label += '<label class="layui-form-label w100"><span class="red">*</span>&nbsp;关联表：</label>';
                div += '<div class="layui-input-inline w180">';
                div += '<select name="relatedTable_' + index + '" lay-filter="relatedTable" lay-verify="required">';
                div += '<option value="">请选择</option>';
                baseutil.AjaxPostMark('/gen/form/listTable', {}, function (list) {
                    if (list != null && list.length > 0) {
                        for (var i = 0; i < list.length; i++) {
                            div += '<option value="' + list[i].tableName + '">' + list[i].tableName + '</option>';
                        }
                    }
                    div += '</select>';
                    div += '</div>';
                    showAndInitSelect(data, label, div);
                });
            } else {
                showAndInitSelect(data, label, div);
            }
        });

        //关联表下拉框
        form.on('select(relatedTable)', function (data) {
            var index = data.elem.attributes['name'].nodeValue.split("_")[1];
            var label1 = '';
            var div1 = '';
            var label2 = '';
            var div2 = '';
            if (data.value !== '') {
                baseutil.AjaxPostMark('/gen/form/listTableField', {tableName: data.value}, function (list) {
                    label1 += '<label class="layui-form-label w100"><span class="red">*</span>&nbsp;关联字段：</label>';
                    div1 += '<div class="layui-input-inline w150">';
                    div1 += '<select name="relatedField_' + index + '" lay-filter="relatedField" lay-verify="required">';
                    div1 += '<option value="">请选择</option>';
                    if (list != null && list.length > 0) {
                        for (var i = 0; i < list.length; i++) {
                            div1 += '<option value="' + list[i].field + '">' + list[i].field + '</option>';
                        }
                    }
                    div1 += '</select>';
                    div1 += '</div>';
                    label2 += '<label class="layui-form-label w100"><span class="red">*</span>&nbsp;关联展示字段：</label>';
                    div2 += '<div class="layui-input-inline w150">';
                    div2 += '<select name="relatedShowField_' + index + '" lay-verify="required">';
                    div2 += '<option value="">请选择</option>';
                    if (list != null && list.length > 0) {
                        for (var i = 0; i < list.length; i++) {
                            div2 += '<option value="' + list[i].field + '">' + list[i].field + '</option>';
                        }
                    }
                    div2 += '</select>';
                    div2 += '</div>';
                    var num = 0;
                    var child = $(data.othis[0].parentNode.parentNode.children).toArray();
                    for (var i = 0; i < child.length; i++) {
                        if (child[i] === data.othis[0].parentNode) {
                            num = i;
                        }
                    }
                    for (var i = num + 1; i < child.length - 3; i++) {
                        child[i] = null;
                    }
                    for (var i = child.length - 1; i > num; i--) {
                        child[i + 4] = child[i];
                    }
                    child[num + 1] = $(label1)[0];
                    child[num + 2] = $(div1)[0];
                    child[num + 3] = $(label2)[0];
                    child[num + 4] = $(div2)[0];
                    $(data.othis[0].parentNode.parentNode).html(child);
                    form.render('select');
                });
            } else {
                showAndInitSelect(data, '', '');
            }
        });

        //上一步
        $('#lastStepBtn').bind('click', function () {
            parent.tabChange('formTableEdit', '/gen/form/getFormTableEdit?tableName=${formTableVo.tableName}', '配置实体', 'formPageEdit');
            return false;
        });

        //预览
        form.on('submit(previewBtn)', function () {
            var param = getSubmitData();
            baseutil.AjaxPostMark('/gen/form/saveOrUpdate', param, function () {
                parent.tabAdd('formPreviewList', '/gen/form/getFormPreviewList?tableName=${formTableVo.tableName}', '预览表格');
            });
            return false;
        });

        //保存
        form.on('submit(saveBtn)', function () {
            var param = getSubmitData();
            baseutil.AjaxPostMark('/gen/form/saveOrUpdate', param, function () {
                dialog.msg('保存成功');
            });
            return false;
        });

        //下一步
        form.on('submit(nextStepBtn)', function () {
            var param = getSubmitData();
            baseutil.AjaxPostMark('/gen/form/saveOrUpdate', param, function () {
                parent.tabChange('formFieldEdit', '/gen/form/getFormFieldEdit?tableName=${formTableVo.tableName}', '配置表单', 'formPageEdit');
            });
            return false;
        });
        
        //取消
        $('#cancel').bind('click', function () {
            parent.tabDelete('formPageEdit');
        });

        function getFormToolData(functionName) {
            var f = $('input[name=' + functionName + ']');
            var str = [];
            for (var i = 0; i < f.length; i++) {
                if (f[i].checked) {
                    str.push(f[i].value);
                } else {
                    str.push('0');
                }
            }
            return str.join(",");
        }

        function getFormFieldData(obj) {
            var formFieldVoList = [];
            var items = $('#' + obj)[0].children;
            for (var i = 0; i < items.length - 1; i++) {
                var formFieldVo = {tableName: '${formTableVo.tableName}', orderNum: (i + 1)};
                var item = items[i].children;
                for (var j = 0; j < item.length; j++) {
                    var tagName = item[j].tagName;
                    if (tagName === 'div'.toUpperCase()) {
                        var name = item[j].children[0].name.split("_")[0];
                        var value = item[j].children[0].value;
                        formFieldVo[name] = value;
                    }
                }
                formFieldVoList.push(formFieldVo);
            }
            return formFieldVoList;
        }

        function getSubmitData() {
            var formTableVo = {tableName: '${formTableVo.tableName}'};
            //工具栏信息
            var formToolVo = {tableName: '${formTableVo.tableName}'};
            formToolVo.addFunction = getFormToolData('addFunction');
            formToolVo.editFunction = getFormToolData('editFunction');
            formToolVo.deleteFunction = getFormToolData('deleteFunction');
            formTableVo.formToolVos = JSON.stringify(formToolVo);
            //搜索栏信息
            formTableVo.formSearchVos = JSON.stringify(getFormFieldData('search'));
            //表格信息
            formTableVo.formShowVos = JSON.stringify(getFormFieldData('show'));
            return formTableVo;
        }
    });

    //添加字段
    function addField(obj, flag) {
        var index = obj.parentNode.parentNode.parentNode.children.length - 1;
        var str = '';
        str += '<div class="layui-form-item">';
        str += '<label class="layui-form-label w100"><span class="red">*</span>&nbsp;字段：</label>';
        str += '<div class="layui-input-inline w120">';
        str += '<input type="text" name="field_' + index + '" lay-verify="required" value="" placeholder="请输入字段" class="layui-input" autocomplete="off">';
        str += '</div>';
        str += '<label class="layui-form-label w100"><span class="red">*</span>&nbsp;名称：</label>';
        str += '<div class="layui-input-inline w120">';
        str += '<input type="text" name="name_' + index + '" lay-verify="required" value="" placeholder="请输入名称" class="layui-input" autocomplete="off">';
        str += '</div>';
        if (flag === 'search') {
            str += '<label class="layui-form-label w100"><span class="red">*</span>&nbsp;类型：</label>';
            str += '<div class="layui-input-inline w120">';
            str += '<select name="type_' + index + '" lay-filter="type" lay-verify="required">';
            str += '<option value="">请选择</option>';
            str += '<option value="input">输入框</option>';
            str += '<option value="select">下拉框</option>';
            str += '<option value="dateTime">日历框</option>';
            str += '</select>';
            str += '</div>';
        } else if (flag === 'show') {
            str += '<label class="layui-form-label w100"><span class="red">*</span>&nbsp;类型：</label>';
            str += '<div class="layui-input-inline w120">';
            str += '<select name="type_' + index + '" lay-filter="type" lay-verify="required">';
            str += '<option value="">请选择</option>';
            str += '<option value="input">文本</option>';
            str += '<option value="select">字典</option>';
            str += '<option value="dateTime">时间</option>';
            str += '</select>';
            str += '</div>';
        }
        str += '<button class="layui-btn layui-btn-normal layui-btn-danger" type="button" onclick="deleteField(this,\'' + flag + '\')">删除</button>';
        if (index === 0) {
            str += '<button class="layui-btn layui-btn-normal layui-btn-disabled" style="margin-left: 9px !important;" type="button" onclick="moveUp(this,\'' + flag + '\')">上移</button>';
            str += '<button class="layui-btn layui-btn-normal layui-btn-disabled" style="margin-left: 9px !important;" type="button" onclick="moveDown(this,\'' + flag + '\')">下移</button>';
        } else {
            var children = obj.parentNode.parentNode.parentNode.children[index - 1].children;
            $(children[children.length - 1]).removeClass("layui-btn-disabled");
            str += '<button class="layui-btn layui-btn-normal" style="margin-left: 9px !important;" type="button" onclick="moveUp(this,\'' + flag + '\')">上移</button>';
            str += '<button class="layui-btn layui-btn-normal layui-btn-disabled" style="margin-left: 9px !important;" type="button" onclick="moveDown(this,\'' + flag + '\')">下移</button>';
        }
        str += '</div>';
        $(obj.parentNode.parentNode).before(str);
        form.render('select');
    }

    //删除字段
    function deleteField(obj, flag) {
        obj.parentNode.remove();
        reloadDiv(flag);
    }

    //上移
    function moveUp(obj, flag) {
        if (!$(obj).hasClass("layui-btn-disabled")) {
            var index = $(obj.parentNode).index();
            var divs = $($('#' + flag)[0].children).toArray();
            var temp = divs[index - 1];
            divs[index - 1] = divs[index];
            divs[index] = temp;
            $('#' + flag).html(divs);
            reloadDiv(flag);
        }
    }

    //下移
    function moveDown(obj, flag) {
        if (!$(obj).hasClass("layui-btn-disabled")) {
            var index = $(obj.parentNode).index();
            var divs = $($('#' + flag)[0].children).toArray();
            var temp = divs[index + 1];
            divs[index + 1] = divs[index];
            divs[index] = temp;
            $('#' + flag).html(divs);
            reloadDiv(flag);
        }
    }

    function reloadDiv(flag) {
        var length = $('#' + flag)[0].children.length - 1;
        for (var i = 0; i < length; i++) {
            var item = $('#' + flag)[0].children[i];
            for (var j = 0; j < item.children.length; j++) {
                var tagName = item.children[j].tagName;
                if (tagName === 'div'.toUpperCase()) {
                    var nodeValue = item.children[j].children[0].attributes['name'].nodeValue;
                    item.children[j].children[0].attributes['name'].nodeValue = nodeValue.split("_")[0] + "_" + (i);
                }
            }
            if (i === 0) {
                $(item.children[item.children.length - 2]).addClass("layui-btn-disabled");
            } else {
                $(item.children[item.children.length - 2]).removeClass("layui-btn-disabled");
            }
            if (i === length - 1) {
                $(item.children[item.children.length - 1]).addClass("layui-btn-disabled");
            } else {
                $(item.children[item.children.length - 1]).removeClass("layui-btn-disabled");
            }
        }
        form.render('select');
    }
</script>
</body>
</html>